﻿using NetworkPower.Data.Models;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NetworkPower
{
    public partial class NavForm : Form
    {
        static NetworkPowerContext context = new NetworkPowerContext();

        public NavForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            var rs = fbd.ShowDialog();
            if (rs == System.Windows.Forms.DialogResult.OK)
            {
                string[] files = Directory.GetFiles(fbd.SelectedPath);
                foreach (string file in files)
                {
                    string fileName = Path.GetFileName(file);
                    if (!isExistLineProtectBox(fileName) && (Path.GetExtension(fileName).Equals(".xls") || Path.GetExtension(fileName).Equals(".xlsx")))
                    {
                        GetFromExcelFile(file);
                    }
                }
                MessageBox.Show("完成保存！");
            }
        }

        private bool isExistLineProtectBox(string fileName)
        {
            return context.Set<LineProtectBox>().Any(a => a.LineProtectBoxName.Equals(fileName));
        }

        public IWorkbook GetWorkBook(string filePath)
        {
            IWorkbook wk = null;
            string extension = System.IO.Path.GetExtension(filePath);
            using (FileStream fs = File.OpenRead(filePath))
            {
                if (extension.Equals(".xls"))
                {
                    wk = new HSSFWorkbook(fs);
                }
                else
                {
                    wk = new XSSFWorkbook(fs);
                }
            }
            return wk;
        }

        private LineProtectBox SaveLineProtectBox(string filePath)
        {
            string fileName = Path.GetFileName(filePath);
            var lineProtectBox = new LineProtectBox()
            {
                LineProtectBoxId = Guid.NewGuid(),
                LineProtectBoxName = fileName
            };
            context.LineProtectBoxs.Add(lineProtectBox);
            context.SaveChanges();
            return context.LineProtectBoxs.Where(s => s.LineProtectBoxId == lineProtectBox.LineProtectBoxId).FirstOrDefault();
        }

        private Department SaveDepartment(string sheetName, int sheetIndex, Guid LineProtectBoxId)
        {
            var depart = new Department()
            {
                DepartmentId = Guid.NewGuid(),
                DepartmentName = sheetName,
                LineProtectBoxId = LineProtectBoxId,
                SheetIndex = sheetIndex
            };
            context.Departments.Add(depart);
            context.SaveChanges();
            return context.Departments.Where(s => s.DepartmentId == depart.DepartmentId).FirstOrDefault();
        }

        public void GetFromExcelFile(string filePath)
        {
            IWorkbook wk = null;
            try
            {
                var lineProtectBox = SaveLineProtectBox(filePath);
                if (lineProtectBox == null)
                    return;

                wk = GetWorkBook(filePath);
                for (int i = 0; i < wk.NumberOfSheets; i++)
                {
                    ISheet sheet = wk.GetSheetAt(i);
                    var loopLine = SaveDepartment(sheet.SheetName, i, lineProtectBox.LineProtectBoxId);
                    if (loopLine == null)
                    {
                        return;
                    }

                    IRow row = sheet.GetRow(0);
                    short PreviousColor = -1;
                    Guid PreviousGuid = Guid.Empty;
                    for (int j = 1; j <= sheet.LastRowNum; j++)
                    {
                        row = sheet.GetRow(j);
                        if (row != null)
                        {
                            LoopLine pc = new LoopLine();
                            pc.RowIndex = j;
                            pc.LoopLineId = Guid.NewGuid();
                            pc.DepartmentId = loopLine.DepartmentId;
                            pc.LoopLineName = row.GetCell(0).ToString();
                            pc.LoopLineNameColor = Common.ColorMapping.GetColor(row.GetCell(0).CellStyle.FillForegroundColor);
                            pc.InternalLine = row.GetCell(1).ToString();
                            pc.TerminalNo = row.GetCell(2).ToString();
                            pc.TerminalNoColor = Common.ColorMapping.GetColor(row.GetCell(2).CellStyle.FillForegroundColor);
                            pc.OuterLine = row.GetCell(3).ToString();
                            pc.LoopNo = row.GetCell(4) == null ? string.Empty : row.GetCell(4).ToString();

                            if (PreviousColor == row.GetCell(2).CellStyle.FillForegroundColor)
                            {
                                pc.ShortConnectTo = PreviousGuid;
                                PreviousColor = -1;
                            }
                            else
                            {
                                PreviousGuid = pc.LoopLineId;
                                PreviousColor = row.GetCell(2).CellStyle.FillForegroundColor;
                            }
                            context.LoopLines.Add(pc);
                        }
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            new DataList().ShowDialog();
        }
    }
}
